Author: SAI K
Core Java | Java Tutorial
Writing your first Java program is a simple and essential step in learning the language. The classic "Hello, World!" program is a basic program that outputs the text "Hello, World!" to the console. This guide will take you through the steps to create, compile, and run your first Java program.
You can use any text editor such as Notepad, Sublime Text, or an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or VS Code.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save the file with a .java
extension. The filename must match the class name. For this example, save the file as HelloWorld.java
.
On Windows, you can open Command Prompt by searching for "cmd" in the Start menu. On macOS/Linux, open Terminal from the Applications folder or by searching for it.
Use the cd
command to change the directory to where you saved your HelloWorld.java
file.
Example command (Windows):
cd path\to\directory
Example command (macOS/Linux):
cd /path/to/directory
Use the javac
command to compile the Java source file:
javac HelloWorld.java
This command will create a file named HelloWorld.class
in the same directory if there are no errors.
Use the java command to run the compiled bytecode:
java HelloWorld
You should see the following output in your console:
Hello, World!
+-------------------------------------------------+
| Step 1: Create a Java Program |
+-------------------------------------------------+
| |
| 1. Open a Text Editor or IDE |
| |
| 2. Write the Source Code |
| |
| public class HelloWorld { |
| public static void main(String[] args) { |
| System.out.println("Hello, World!"); |
| } |
| } |
| |
| 3. Save the File |
| Filename: HelloWorld.java |
| |
+-------------------------------------------------+
|
v
+-------------------------------------------------+
| Step 2: Compile the Java Program |
+-------------------------------------------------+
| |
| 1. Open Command Prompt or Terminal |
| |
| 2. Navigate to the Directory |
| cd /path/to/directory |
| |
| 3. Compile the Program |
| javac HelloWorld.java |
| |
| 4. Output: HelloWorld.class |
| |
+-------------------------------------------------+
|
v
+-------------------------------------------------+
| Step 3: Run the Java Program |
+-------------------------------------------------+
| |
| 1. Execute the Bytecode |
| java HelloWorld |
| |
| 2. View the Output |
| Hello, World! |
| |
+-------------------------------------------------+
Writing your first Java program is straightforward. It involves creating the source code, compiling it into bytecode, and then running it using the JVM. The "Hello, World!" program is a great way to get started with Java and understand the basics of writing, compiling, and running Java code.
.java
extension.javac
command to compile the source code into bytecode.java
command to execute the compiled bytecode.This text-based diagram should now be aligned properly, providing a clear visual representation of the steps to create, compile, and run a Java program.
By mastering these steps, you can confidently begin your journey into Java programming and build more complex applications over time.